package com.app.utils.base.j2se;


import java.lang.reflect.Method;

public class PropertyFieldValue {
	private Object objInstance;

	public PropertyFieldValue(Object obj) {
		this.objInstance = obj;
	}

	public Object getPropertyFieldValue(String name) {
		if(name == null || "".equals(name)){
			return null;
		}
		
		boolean isBoolean = false;
		Method [] methods = objInstance.getClass().getMethods();
		for(int i=0; i<methods.length; i++){
			if(methods[i].getName().toLowerCase().endsWith(name.toLowerCase())){
				if("boolean".equals(methods[i].getReturnType().toString())){
					if(methods[i].getName().toLowerCase().equals(("is"+name).toLowerCase())){
						name = name.toLowerCase();
						name = "is"+ name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
						isBoolean = true;
						break;
					}
				}
			}
		}
		if(!isBoolean){
//			name = name.toLowerCase();
			name = "get" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
		}
		return invoke(name);
	}

	/**
	 * 设置Model类属性
	 * @param methodName 方法名
	 * @param values 值
	 */
	private Object invoke(String methodName) {
		Object obj = null;
		// 执行方法
		Method execMethod;
		try {
			// 取得执行方法对象
			execMethod = objInstance.getClass().getMethod(methodName);
			// 执行方法
			obj = execMethod.invoke(objInstance, new Object[] { });
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
}
